//
//  main.m
//  07-homeWork-MRC
//
//  Created by 402030900@qq.com on 16/7/12.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Zone.h"
int main(int argc, const char * argv[]) {
#pragma mark -- 第一题
#if 0
  NSString * path = @"/Users/402030900qq.com/DLS160710/Objective-C/HomeWork/07-homeWork-MRC/07-homeWork-MRC/main.m";
    NSString * source = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray * sourceArray = [source componentsSeparatedByString:@"\n"];
    NSMutableArray * allArray = [NSMutableArray array];
    for (NSString * area in sourceArray) {
        if (![area hasPrefix:@" "]) {
//            NSLog(@"省");
            Province * pro = [Province zoneWithName:area];
            [allArray addObject:pro];
        } else if (![area hasPrefix:@"   "]) {
//            NSLog(@"市");
            Province * pro = [allArray lastObject];
            City * city = [City zoneWithName:area];
            [pro.array addObject:city];
            
        } else {
//            NSLog(@"区");
            City * city = [[[allArray lastObject] array] lastObject];
            Zone * zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@",allArray);
#endif
#pragma mark --第二题
    NSString * path = @"/Users/402030900qq.com/DLS160710/Objective-C/HomeWork/07-homeWork-MRC/07-homeWork-MRC/Location1.txt";
    NSString * source = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray * sourceArray = [source componentsSeparatedByString:@","];
    NSMutableArray * allArray = [NSMutableArray array];
    for (NSString * area in sourceArray) {
        NSArray * lineArray = [area componentsSeparatedByString:@"|"];
        TempCity * tempCity = [[TempCity alloc]initWithName:[lineArray firstObject] Number:[lineArray lastObject]];
        [allArray addObject:tempCity];
        [tempCity release];
        NSSortDescriptor * sort = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
        [allArray sortUsingDescriptors:@[sort]];
        [sort release];
    }
        NSMutableArray * finalArray = [NSMutableArray array];
        for (TempCity * temp in allArray) {
            if ([temp.number hasSuffix:@"0000"]) {
                Province * pro = [Province zoneWithTempCity:temp];
                [finalArray addObject:pro];
            } else if ([temp.number hasSuffix:@"00"] && ![temp.number hasSuffix:@"0000"]){
                Province * pro = [finalArray lastObject];
                City * city = [City zoneWithTempCity:temp];
                [pro.array addObject:city];
            } else{
                Province * pro = [finalArray lastObject];
                City * city = [pro.array lastObject];
                Zone * zone = [Zone zoneWithTempCity:temp];
                [city.array addObject:zone];
            }
        }
               NSLog(@"%@",finalArray);
    return 0;
}
